#!/bin/env python3

import sys
from tqdm import tqdm
import requests
import os
import gzip
import re

mirror = "http://mirrors.163.com"
repos = ['main']

def download_package(name, urls):
    if not os.path.exists(name):
        os.makedirs(name)
    for l in urls:
        path = "%s/%s" %(name, os.path.basename(l))
        res = requests.get(l, stream=True)
        with open(path, 'wb') as f:
            for data in tqdm(
                res.iter_content(chunk_size=1024),
                total=int(int(res.headers['Content-Length']) / 1024),
                unit='kb',
                leave=True,
                desc="Downloading %s Source" % name,
                ):
                pass
                f.write(data)

def download(url, dest):
    if not os.path.exists(dest):
        os.makedirs(dest)
    b = os.path.basename(url)
    path = "%s/%s" %(dest, b)
    res = requests.get(url, stream=True)
    with open(path, 'wb') as f:
        for data in tqdm(
            res.iter_content(chunk_size=1024),
            total=int(int(res.headers['Content-Length']) / 1024),
            unit='kb',
            leave=True,
            desc="Downloading %s " % b
            ):
            pass
            f.write(data)

if len(sys.argv) < 3:
    print("%s <distro> <version>" % sys.argv[0])
    sys.exit(1)


distro = sys.argv[1]
ver = sys.argv[2]

for repo in repos:
    sl = mirror + r'/' + distro + '/dists/' + ver + r'/' + repo + r'/' + 'source' + r'/' + 'Sources.gz'
    dpath = distro + r'/' + ver + r'/' + repo

    print("Sources file save to: %s/Sources.gz" % dpath)
    download(sl, dpath)

    pf = dpath + r'/' + 'Sources.gz'
    print("reading debian package file: %s ..." % pf)

    f = gzip.open(pf, 'rb')
    lines = f.readlines()
    f.close()
    lines = [ i.decode('utf-8') for i in lines ]

    sdir = dpath + r'/' + 'Source'
    if not os.path.exists(sdir):
        os.makedirs(sdir)

    os.chdir(sdir)
    print("save source to: %s" % sdir)
    # begin download
    directory = ""
    files = []
    record = False
    begin = False
    package = ""
    keystart = re.compile(r'^[A-Z][a-z1-9]+[-]?[a-zA-Z1-9]+:')
    for line in lines:
        if line.startswith("Package:"):
            begin = True
            name = line.split(':')[1].strip()
            package = name
            #print("Package name: %s"% name)

        if not begin:
            continue

        if line.startswith("Directory:"):
            directory = line.split(':')[1].strip()
            if record == False and files != []:
                begin = False
                #print("case 2 files:")
                files_new = []
                # replace directory
                for i in files:
                    fn = i.replace('DirToReplace', directory)
                    files_new.append(fn)
                directory = ""
                #for i in files_new:
                #    print(i)
                download_package(package, files_new)
                package = ""
                files = []
                continue

        if line.startswith("Files:"):
            #print("start record")
            record = True
            continue

        if record and keystart.match(line) != None:
            #print("stop record")
            record = False
            if directory != "":
                directory = ""
                begin = False
                #print("case 1 files:")
                #for i in files:
                #    print(i)
                download_package(package, files)
                package = ""
                files = []
                continue

        if record:
            #print("line: %s"% line)
            n = line.split(' ')[3].strip()
            if directory != "":
                files.append("%s/%s/%s"% (mirror + r'/' + distro, directory, n))
            else:
                files.append("%s/%s/%s"% (mirror + r'/' + distro, 'DirToReplace', n))
